package com.goparry.taskman;

import com.goparry.taskman.aws.AWSUtils;
import com.goparry.taskman.browse.BrowseActivity;
import com.goparry.taskman.home.HomeActivity;
import com.goparry.taskman.inbox.InboxActivity;
import com.goparry.taskman.maps.MapsActivity;
import com.goparry.taskman.restclient.RESTUtilities;
import com.goparry.taskman.search.SearchActivity;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Do all the app initialization here:
        //   Setup the RESTClient
		RESTUtilities.setServer(RESTUtilities.SERVER.GAE);
		// Setup Amazon s3
	    AWSUtils.initialize(this);


        
        // Main task here is to create the TabHost and add the Activities for each of the Tabs
        
        Resources res = getResources();	//Resource object to get Drawables
        TabHost tabHost = getTabHost();	// The activity TabHost
        TabSpec tabSpec;	//Reusable TabSpec for each tab
        Intent intent;		//Reusable Intent for each tab
        
        //Create an Intent to launch the Home activity for the tab
        intent = new Intent().setClass(this, HomeActivity.class);
        
        //Initialize a TabSpec for the tab and add it to the TabHost
        tabSpec = tabHost.newTabSpec("home").setIndicator("Home",res.getDrawable(R.drawable.ic_tab_home)).setContent(intent);
        tabHost.addTab(tabSpec);
        
        //Create an Intent to launch the Inbox activity for the tab
        intent = new Intent().setClass(this, InboxActivity.class);
        
        //Initialize a TabSpec for the tab and add it to the TabHost
        tabSpec = tabHost.newTabSpec("inbox").setIndicator("Inbox",res.getDrawable(R.drawable.ic_tab_inbox)).setContent(intent);
        tabHost.addTab(tabSpec);
        
        //Create an Intent to launch the Browse activity for the tab
        intent = new Intent().setClass(this, BrowseActivity.class);
        
        //Initialize a TabSpec for the tab and add it to the TabHost
        tabSpec = tabHost.newTabSpec("browse").setIndicator("Browse",res.getDrawable(R.drawable.ic_tab_browse)).setContent(intent);
        tabHost.addTab(tabSpec);

        //Create an Intent to launch the Browse activity for the tab
        intent = new Intent().setClass(this, MapsActivity.class);
        
        //Initialize a TabSpec for the tab and add it to the TabHost
        tabSpec = tabHost.newTabSpec("maps").setIndicator("Maps",res.getDrawable(R.drawable.ic_tab_maps)).setContent(intent);
        tabHost.addTab(tabSpec);

    }
	public boolean onCreateOptionsMenu(Menu menu) {
		// Add the menu from the home_menu resource "res/menu/home_menu"
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle menu item selection
		// - barcode
		// - settings
		// - search
		switch (item.getItemId()) {
		case R.id.barcode:
			// Make a barcode reader request
			if (TaskManUtils.isIntentAvailable(getApplicationContext(), "com.google.zxing.client.android.SCAN")) {
		        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		        startActivityForResult(intent, GLOBAL.SCAN_REQUEST_CODE);
			return true;
			} else {
				return false;
			}
		case R.id.settings:
			// Select the server to be used
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Select Server");
			builder.setSingleChoiceItems(R.array.serverOptions, RESTUtilities.getServerNumber(), new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int item) {
			        Resources res = getResources();
			    	TypedArray items = res.obtainTypedArray(R.array.serverOptions);
			    	if (items.getString(item).equalsIgnoreCase("Google App Engine")) {
			    		RESTUtilities.setServer(RESTUtilities.SERVER.GAE);
			        } else if (items.getString(item).equalsIgnoreCase("Goparry Dev Server")) {
			        	RESTUtilities.setServer(RESTUtilities.SERVER.DEV);
			        }
			        dialog.dismiss();
			    }
			});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		case R.id.search:
			// Request a search
			onSearchRequested();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	    // Handle the return from external application requests
		if (requestCode == GLOBAL.SCAN_REQUEST_CODE) {
	        // Handle the request from the barcode scanner
			if (resultCode == RESULT_OK) {
	            String contents = intent.getStringExtra("SCAN_RESULT");
		        Bundle barcodeBundle = new Bundle();
		        barcodeBundle.putString("BARCODE", contents);
		        Intent searchIntent = new Intent().setClass(this, SearchActivity.class);
		        searchIntent.setAction("BARCODE_SEARCH");
		        searchIntent.putExtras(barcodeBundle);
		        startActivity(searchIntent);
	        } else if (resultCode == RESULT_CANCELED) {
	        	// Do nothing
	        }
	    }
	}

}